package cn.itcast.usermanager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanager.mapper.NewUserMapper;
import cn.itcast.usermanager.mapper.UserMapper;
import cn.itcast.usermanager.pojo.EasyUIResult;
import cn.itcast.usermanager.pojo.User;
import cn.itcast.usermanager.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private NewUserMapper userMapper;
	
	@Override
	public List<User> queryUserAll() {
		List<User> users = userMapper.select(null);
		return users;
	}

	@Override
	public EasyUIResult queryByPage(int pageNum, int pageSize) {
		//在查询方法调用之前,调用分页插件的静态方法,中间最好不要隔任何代码
		PageHelper.startPage(pageNum, pageSize);
		User record = new User();
		List<User> userList = userMapper.select(record);
		//初始化pageInfo对象,所有分页参数都可以在pageInfo中获取
		PageInfo<User> pageInfo = new PageInfo<User>(userList);
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setTotal(pageInfo.getTotal());
		easyUIResult.setRows(pageInfo.getList());
		
		return easyUIResult;
	}

	@Override
	public void addUser(User user1, User user2) {
		try {
			userMapper.insert(user1);
			//int a = 9/0;
			userMapper.insert(user2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	@Override
	public boolean save(User user) throws Exception {
		int count = userMapper.insert(user);
		if (count>0) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean delete(List<Object> ids) throws Exception {
		Example example = new Example(User.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id", ids);
			int count = userMapper.deleteByExample(example);
			if (count>0) {
				return true;
			}
			return false;
		
		
	}

	@Override
	public Boolean update(User user) throws Exception {
		
		return userMapper.updateByPrimaryKeySelective(user)>0;
	}

	@Override
	public User queryUserById(Long id) {
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}
	
}
